//
//  YZLNewsListCell.m
//  Car
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 岳明. All rights reserved.
//

#pragma mark - *主视图的 tableViewCell*

#import "YZLNewsListCell.h"
#import "YZLNewsListModel.h"
#import "Macro.h"
#import <UIImageView+WebCache.h>

@interface YZLNewsListCell ()

@property (nonatomic, strong) UIImageView *leftImageView; /**< 图片 */
@property (nonatomic, strong) UILabel *titleLabel; /**< 标题 */
@property (nonatomic, strong) UILabel *dateLabel; /**< 日期 */
@property (nonatomic, strong) UILabel *typeLabel; /**< 类型 */
@property (nonatomic, strong) UILabel *hotLabel; /**< 热门 */
@property (nonatomic, assign) BOOL isHot;

@end

@implementation YZLNewsListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 创建图片
        self.leftImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.leftImageView];
        // 设置弧边
        self.leftImageView.layer.cornerRadius = 5;
        self.leftImageView.layer.masksToBounds = YES;
        
        // 创建标题
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        // 设置 label 的行数
        self.titleLabel.numberOfLines = 0;
//        self.titleLabel.font = [UIFont systemFontOfSize:15];
        
        // 创建日期
        self.dateLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.dateLabel];
        // 修改 label 的字体颜色和大小
        self.dateLabel.textColor = [UIColor grayColor];
        self.dateLabel.font = [UIFont systemFontOfSize:15];
        
        // 创建类型
        self.typeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_typeLabel];
        _typeLabel.textColor = [UIColor grayColor];
        _typeLabel.font = [UIFont systemFontOfSize:15];
        // 修改 label 右对齐
        _typeLabel.textAlignment = NSTextAlignmentRight;
        
        // 创建热门
        self.hotLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.hotLabel];
        // 设置 label 的底色和字体颜色以及字体大小，并居中
        self.hotLabel.backgroundColor = [UIColor colorWithRed:23 / 255.f green:169 / 255.f blue:200 / 255.f alpha:1];
        self.hotLabel.textColor = [UIColor whiteColor];
        self.hotLabel.font = [UIFont systemFontOfSize:12.5];
        self.hotLabel.textAlignment = NSTextAlignmentCenter;
        // 设置 label 的弧角
        self.hotLabel.layer.cornerRadius = 3;
        self.hotLabel.layer.masksToBounds = YES;
        self.hotLabel.text = @"热门"; /**< 否则只显示热门 */
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.leftImageView.frame = CGRectMake(15, 10, (self.contentView.frame.size.height - 20) * 1.25, self.contentView.frame.size.height - 20);
    
    
    _titleLabel.frame = CGRectMake(25 + self.leftImageView.frame.size.width, 10, kWidth - 125, (self.contentView.frame.size.height - 20) * 2 / 3);
    
    if (self.isHot) {
        _dateLabel.frame = CGRectMake(25 + self.leftImageView.frame.size.width, (self.contentView.frame.size.height - 20) * 2 / 3 + 10, (kWidth - 120) / 2, (self.contentView.frame.size.height - 20) / 3);
        _typeLabel.frame = CGRectMake(kWidth / 2, _dateLabel.frame.origin.y, kWidth / 2 - 15, _dateLabel.frame.size.height);
        self.hotLabel.frame = CGRectMake(0, 0, 0, 0);
    } else {
        _dateLabel.frame = CGRectMake(0, 0, 0, 0);
        _typeLabel.frame = CGRectMake(0, 0, 0, 0);
         _hotLabel.frame = CGRectMake(kWidth - 45, (self.contentView.frame.size.height - 20) * 2 / 3 + 10, 30, 15);
    }
}

- (void)setModel:(YZLNewsListModel *)model {
    if (_model != model) {
        _model = model;
    }
    
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.newsImage] placeholderImage:[UIImage imageNamed:@"D0"]];
    _titleLabel.text = model.newsTitle;
    
    // 计算出当前时间，并改为北京时间
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[model.newsCreateTime doubleValue] + 8 * 3600];
    
    // 显示时间同时显示新闻类型
    _dateLabel.text = [[NSString stringWithFormat:@"%@", date] substringToIndex:10];
    _typeLabel.text = model.newsCategory;
    [_hotLabel removeFromSuperview];
    
    // 判断是否需要显示时间
    if (model.newsCategory != nil) {
        _isHot = YES;
    } else _isHot = NO;
 
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
